package top.zhangjianyong.blog.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import top.zhangjianyong.blog.cache.ApiCache;
import top.zhangjianyong.blog.entity.po.VisitLog;
import top.zhangjianyong.blog.mapper.VisitLogMapper;
import top.zhangjianyong.blog.service.VisitLogService;
import top.zhangjianyong.blog.utils.SessionUtil;

/**
 * 访问日志服务实现类
 * 记录访客访问信息，包含IP归属地等详细信息
 *
 * @author zhangjianyong
 * @since 2025/4/28 15:03
 */
@Slf4j
@Service
public class VisitLogServiceImpl extends ServiceImpl<VisitLogMapper, VisitLog>
    implements VisitLogService {

    @Override
    public boolean save(String uuid) {
        ApiCache apiCache = ApiCache.get();
        String ip = apiCache.getIp();
        VisitLog visitLog = new VisitLog();
        visitLog.setSession(SessionUtil.getId());
        visitLog.setUserAgent(apiCache.getUserAgent());
        visitLog.setIp(ip);
        visitLog.setIsDel(0);
        visitLog.setUuid(uuid);
        return save(visitLog);
    }

    @Override
    public boolean exists(String uuid) {
        return lambdaQuery().eq(VisitLog::getUuid, uuid).eq(VisitLog::getIsDel, 0).exists();
    }
}
